﻿/* The MIT License:

Copyright (c) 2015 Ivan Gagis <igagis@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. */

// Home page: http://stob-cs.googlecode.com


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using stob;

namespace stob
{
	public class Parser
	{
		private enum EState{
			IDLE,
			UNQUOTED_STRING,
			QUOTED_STRING,
			SINGLE_LINE_COMMENT,
			MULTILINE_COMMENT
		}

		public class Listener
		{
			public Action<string> OnStringParsed;
			public Action OnChildrenParseStarted;
			public Action OnChildrenParseFinished;
		}

		public static void Parse(StreamReader reader, Listener listener)
		{
			EState state = EState.IDLE;

			char prevChar = '\0';

			bool stringHasJustBeenParsed = false;

			StringBuilder curString = new StringBuilder();

			while(!reader.EndOfStream)
			{
				char c = (char)reader.Read();

				switch (state) {
				case EState.IDLE:
					switch (c) {
					case '/':
						if(prevChar == '/') {
							state = EState.SINGLE_LINE_COMMENT;
						}
						//do not append the character here yet
						break;
					case '*':
						if (prevChar == '/') {
							state = EState.MULTILINE_COMMENT;
						} else {
							curString.Append (c);
						}
						break;
					case ' ':
					case '\n':
					case '\r':
					case '\t':
						break;
					case '{':
						if (!stringHasJustBeenParsed) {
							listener.OnStringParsed("");
						}
						stringHasJustBeenParsed = false;
						listener.OnChildrenParseStarted();
						break;
					case '}':
						stringHasJustBeenParsed = false;
						listener.OnChildrenParseFinished ();
						break;
					case '"':
						state = EState.QUOTED_STRING;
						break;
					default:
						if (prevChar == '/') {
							curString.Append ('/');
						}
						curString.Append (c);
						state = EState.UNQUOTED_STRING;
						break;
					}
					break;

				case EState.UNQUOTED_STRING:
					switch (c) {
					case '/':
						if (prevChar == '/') {
							state = EState.SINGLE_LINE_COMMENT;
						}
						//do not append the character here yet
						break;
					case '*':
						if (prevChar == '/') {
							state = EState.MULTILINE_COMMENT;
						} else {
							curString.Append (c);
						}
						break;
					case '"':
						listener.OnStringParsed (curString.ToString ());
						curString = new StringBuilder ();
						state = EState.QUOTED_STRING;
						stringHasJustBeenParsed = true;
						break;
					case ' ':
					case '\r':
					case '\n':
					case '\t':
						listener.OnStringParsed (curString.ToString ());
						curString = new StringBuilder ();
						state = EState.IDLE;
						stringHasJustBeenParsed = true;
						break;
					case '{':
						listener.OnStringParsed (curString.ToString ());
						curString = new StringBuilder ();
						state = EState.IDLE;
						listener.OnChildrenParseStarted ();
						stringHasJustBeenParsed = false;
						break;
					case '}':
						listener.OnStringParsed (curString.ToString ());
						curString = new StringBuilder ();
						state = EState.IDLE;
						stringHasJustBeenParsed = false;
						listener.OnChildrenParseFinished ();
						break;
					default:
						if (prevChar == '/') {
							curString.Append ('/');
						}
						curString.Append (c);
						break;
					}
					break;
				
				case EState.QUOTED_STRING:
					switch (c) {
					case '"':
						if (prevChar == '\\') {
							curString.Append ('\"');
						} else {
							listener.OnStringParsed (curString.ToString ());
							curString = new StringBuilder ();
							state = EState.IDLE;
							stringHasJustBeenParsed = true;
						}
						break;
					case '\\':
						if (prevChar == '\\') {
							curString.Append ('\\');
							c = '\0';//this is to make new prevChar not to be '\' next cycle
						}
						break;
					case '\r':
					case '\n':
					case '\t':
						//ignore
						break;
					default:
						if (prevChar == '\\') {//if escape sequence
							switch (c) {
							case 'r':
								curString.Append ('\r');
								break;
							case 'n':
								curString.Append ('\n');
								break;
							case 't':
								curString.Append ('\t');
								break;
							default:
								//ignore
								break;
							}
						}else{
							curString.Append(c);
						}
						break;
					}
					break;
				case EState.SINGLE_LINE_COMMENT:
					if (c == '\n') {
						state = EState.IDLE;
					}
					break;
				case EState.MULTILINE_COMMENT:
					if (c == '/' && prevChar == '*') {
						state = EState.IDLE;
						c = '\0';//this is to make new prevChar not to be '/' next cycle
					}
					break;
				default:
					//Assert(false)
					break;
				}

				prevChar = c;
			}//~while(not end of stream)

			switch (state) {
			case EState.IDLE:
				//do nothing
				break;
			case EState.UNQUOTED_STRING:
				listener.OnStringParsed (curString.ToString ());
				break;
			default:
				throw new InvalidDataException ();
			}
		}
	}
}
